<template>
    <div :id="id"></div>
</template>

<script>
import register from '@/js/main/register.js';

export default {
    props: {
        element: {
            type: Object
        },
        id: {
            type: String
        }
    },
    data() {
        return {
            window: window,
            viewer: null,
            pmtsManager: null,
            imageManager: null,
            terrainManager: null
        };
    },
    created() {
        register.addComponent(this);
    },
    watch: {},
    components: {},
    mounted() {
        this.createMapPanel();
    },
    destroyed() {
        register.removeComponent(this);
    },
    methods: {
        redraw() {
            this.setGlobalParam();
        },
        createMapPanel() {
            this.viewer = Freedo.FdApp.createDefaultViewer(this.id);
            this.viewer.scene.requestRenderMode = false;
            this.pmtsManager = new Freedo.FdTools.FdPMTSManager(this.viewer);
            this.imageManager = new Freedo.FdTools.FdImageryLayersManager(this.viewer);
            this.terrainManager = new Freedo.FdTools.FdTerrain(this.viewer);
            this.setGlobalParam();
        },
        setGlobalParam() {
            let pmtsUrl = this.element.data.pmtsUrl;
            let wmtsUrl = this.element.data.wmtsUrl;
            let wetsUrl = this.element.data.wetsUrl;
            this.pmtsManager.removeAll();
            this.imageManager.removeAll();
            let autoFly = true;
            if (pmtsUrl) {
                let urls = pmtsUrl.split(',');
                for (let i = 0; i < urls.length; i++) {
                    this.pmtsManager.add({
                        id: '' + (i + 1),
                        url: urls[i],
                        flytoOrGoto: false,
                        autoFlyto: autoFly
                    });
                    autoFly = false;
                }
            }
            if (wmtsUrl) {
                let urls = wmtsUrl.split(',');
                for (let i = 0; i < urls.length; i++) {
                    this.imageManager.addByPMTS({
                        url: urls[i],
                        autoFly: autoFly
                    });
                    autoFly = false;
                }
            }
            if (wetsUrl) {
                this.terrainManager.setTerrain(wetsUrl, 'WETS');
            } else {
                this.terrainManager.dispose();
            }
        }
    }
};
</script>
<style lang='scss' scoped>
.backGround {
    width: 100%;
    height: 100%;
}
.panel {
    position: absolute;
    top: 0px;
    opacity: 0.01;
    width: 100%;
    height: 100%;
}
</style>